package com.nie.orderfood.controller;

import com.nie.orderfood.DTO.OrderMasterDTO;
import com.nie.orderfood.Utils.ResultUtils;
import com.nie.orderfood.VO.ResultVO;
import com.nie.orderfood.convertor.Orderform2OrderMasterDTOConvertor;
import com.nie.orderfood.enums.ResultEnum;
import com.nie.orderfood.exception.SellException;
import com.nie.orderfood.form.OrderForm;
import com.nie.orderfood.service.BuyerService;
import com.nie.orderfood.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: orderfood
 * @description: 买家端订单表现层
 * @author: NieMiao
 * @create: 2018-10-15 16:29
 **/
@RestController
@RequestMapping("/buyer/order")
public class BuyerOrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private BuyerService buyerService;
    //创建订单
    @PostMapping("/create")
    public ResultVO<Map<String,String>> create(@Valid OrderForm orderForm,
                                               BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            throw new SellException(ResultEnum.PARAM_ERROR.getCode(),
                    bindingResult.getFieldError().getDefaultMessage());
        }
            OrderMasterDTO orderMasterDTO = Orderform2OrderMasterDTOConvertor.convert(orderForm);
        if (CollectionUtils.isEmpty(orderMasterDTO.getOrderDetailList())) {
            throw new SellException(ResultEnum.ORDER_EMPTY_ERROR);
        }
        OrderMasterDTO order = orderService.createOrder(orderMasterDTO);
        Map map = new HashMap();
        map.put("orderId",order.getOrderId());
        return ResultUtils.success(map);
    }

    //查询订单列表
    @GetMapping("/list")
    public ResultVO<List<OrderMasterDTO>> list(@RequestParam(value = "openid") String openid,
                                               @RequestParam(value = "page",defaultValue = "0")Integer page,
                                               @RequestParam(value = "size",defaultValue = "10")Integer size) {
        if (StringUtils.isEmpty(openid)) {
            throw new SellException(ResultEnum.PARAM_ERROR);
        }
        PageRequest pageRequest = new PageRequest(page,size);
        Page<OrderMasterDTO> list = orderService.findList(openid, pageRequest);
        return ResultUtils.success(list.getContent());
    }
    //查询订单详情
    @GetMapping("/detail")
    public  ResultVO<OrderMasterDTO> detail(@RequestParam(value = "openid") String openid,
                                            @RequestParam(value = "orderId")String orderId) {
        OrderMasterDTO orderMasterDTO = buyerService.findOrderOne(openid, orderId);
        return ResultUtils.success(orderMasterDTO);
    }
    //取消订单
    @PostMapping("/cancel")
    public  ResultVO cancel(@RequestParam(value = "openid") String openid,
                                            @RequestParam(value = "orderId")String orderId){
        buyerService.cancelOrder(openid,orderId);
        return ResultUtils.success();
    }
}
